<template>
    <d2-container>

        <el-form :inline="true" :model="dataSearch" ref="dataSearch">

            <el-form-item label="商品名称" prop="name">
                <el-input v-model="dataSearch.name" placeholder="请输入商品名称"></el-input>
            </el-form-item>

            <el-form-item label="商品种类" prop="category">
                <el-select v-model="dataSearch.category" clearable placeholder="可选择种类">
                    <el-option
                            v-for="item in categoryList"
                            :key="item._id"
                            :label="item.name"
                            :value="item._id"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="校区" prop="school" v-if='showSchool'>
                <el-select
                        v-model="dataSearch.school"
                        filterable
                        placeholder="请选择校区"
                        @change="schoolSelected"
                        :disabled="disabled"
                >
                    <el-option
                            v-for="item in schoolList"
                            :key="item._id"
                            :label="item.name"
                            :value="item._id"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="formSubmit">
                    <d2-icon name="search"/>
                    查询
                </el-button>
            </el-form-item>

            <el-form-item>
                <el-button @click="formReset">
                    <d2-icon name="refresh"/>
                    重置
                </el-button>
            </el-form-item>

            <a href="#/product-new">
                <el-button type="primary" style="float:right" icon="el-icon-plus">新建商品</el-button>
            </a>
        </el-form>
        <el-table :data="productList" border style="width: 100%">
            <el-table-column prop="name" label="商品名称"></el-table-column>
            <el-table-column prop="productCategory" label="商品种类"></el-table-column>
            <el-table-column prop="school.name" label="校区"></el-table-column>
            <el-table-column prop="costPrice" label="成本价(元)"></el-table-column>
            <el-table-column prop="wholeSalePrice" label="批发价(元)" v-if="(headquarter == 1) || (headquarter == 2)"></el-table-column>
            <el-table-column prop="sellingPrice" label="零售价(元)"></el-table-column>
            <el-table-column prop="storage" label="库存"></el-table-column>
            <el-table-column fixed="right" label="操作">
                <template slot-scope="scope">
                    <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
                    <el-button @click="deleteClick(scope.row)" type="text" size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                background
                layout="total,sizes,prev, pager, next,jumper"
                :page-size="pageSize"
                :current-page="currentPage"
                :total="total"
                @current-change="handleCurrentChange"
                @size-change="handlerSizeChange"
                @next-click="handleNextPage"
                @prev-click="handlePrevClick"
        ></el-pagination>
    </d2-container>
</template>

<script>
    import request from "@/plugin/axios";
    import {mapState} from "vuex";
    export default {
        data() {
            return {
                dataSearch: {
                    name: "",
                    category: "",
                    school : undefined
                },
                productList: [],
                categoryList: [],
                total: 0,
                pageSize: 20,
                currentPage: 1,
                showSchool :false

            };
        },
        computed: {
            ...mapState('d2admin/user', ['info']),
        },
        methods: {
            formSubmit() {
                this.getProductList(1, this.pageSize);
            },
            formReset() {
                this.$refs.dataSearch.resetFields();
                this.getProductList(1,this.pageSize)
                this.dataSearch.school = this.school
            },
            handleClick(e) {
                this.$router.push({path: "/product-detail?id=" + e._id});
            },
            deleteClick(e) {
                this.$confirm("此操作将删除该商品, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        request({
                            url: "/product/deleteProduct",
                            method: "post",
                            data: {id: e._id}
                        }).then(res => {
                            if (res.code == 200) {
                                this.$message({
                                    message: "删除成功"
                                });
                                this.getProductList(1, this.pageSize);
                            }
                        });
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除"
                        });
                    });
            },
            handleCurrentChange(e) {
                this.getProductList(e, this.pageSize);
            },
            handlerSizeChange(e) {
                this.pageSize = e;
                this.getProductList(1, this.pageSize);
            },
            handleNextPage(e) {
                this.getProductList(e, this.pageSize);
            },
            handlePrevClick(e) {
                this.getProductList(e, this.pageSize);
            },
            getProductList(page, pageSize) {
                let data = {};
                if (this.dataSearch.name != "") {
                    data.name = this.dataSearch.name;
                }
                if (this.dataSearch.category != "") {
                    data.category = this.dataSearch.category;
                }
                (data.page = page), (data.pageSize = pageSize);
                data.school = this.dataSearch.school
                console.log(data,'sds')
                request({
                    url: "/product/getProductList",
                    method: "post",
                    data
                }).then(res => {

                    this.productList = res.data.product;
                    this.productList.forEach((product, index) => {
                        this.productList[index].productCategory = product.productCategory.map((value => value.name || '')).join(',')
                    })
                    // let index = this.productList.indexOf('批发价')

                    this.total = res.data.count;
                    // 返回数据
                });
            },

            getCategoryList(pageSize) {
                request({
                    url: "/productCategory/getProductCategoryList",
                    method: "post",
                    data: {pageSize: pageSize,school:this.dataSearch.school}
                }).then(res => {
                    console.log(res.data.productCategory,'dsxxx')
                    this.categoryList = res.data.productCategory;
                });
            },

            getSchoolList() {

                let data = {};
                data.page = 1
                data.pageSize = 999999
                request({
                    url: "/school/getSchoolList",
                    method: "post",
                    data
                }).then(res => {
                    this.schoolList = res.data.school;
                    // 返回数据
                });

            },
            load(){
                this.school = this.info.school[0]._id
                this.dataSearch.school  = this.info.school[0]._id
                this.getCategoryList(-1);
                this.rights = this.info.rights.map(function(e){return e.name})
                let index1 = this.rights.indexOf('root')
                if ((index1 != -1) || ( this.info.school[0].headquarter == 1) || ( this.info.school[0].headquarter == 2)){
                    this.headquarter = 1
                    this.showSchool = true
                    this.getSchoolList()
                }
                console.log(this.headquarter,'heheh')
                this.getProductList(1, this.pageSize);
            }
        },

        beforeRouteEnter(to,from,next){
            console.log(to.meta,'sdds')
            if (to.meta.refresh == 'a'){
                to.meta.refresh = undefined
                next(v=>{
                v.load()
                })
            }
            else{
                next()
            }

        },

        created() {
            this.load()
        }
    };
</script>
